package org.example.controller;

import com.github.pagehelper.PageInfo;
import org.example.common.result.Result;
import org.example.entity.Article;
import org.example.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 增
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Article article) {
            articleService.add(article);
            return Result.success();
    }

    /**
     * 删除
     *
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        articleService.delete(id);
        return Result.success();
    }
    /**
     * 批量删除
     */
    @DeleteMapping("/deleteBatch")
    public  Result deleteBatch(@RequestBody List<Integer> ids) {
        articleService.deleteBatch(ids);
        return Result.success();
    }
    /**
     * 改
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Article article) {
        articleService.update(article);
        return Result.success();
    }

    /*
    查询所有的员工
     */

    @GetMapping("/selectAll")
    public Result selectAll(Article article){
        List<Article> list=articleService.selectAll(article);
         return Result.success(list);
    }

    /**
     * 查询特定id的员工
     * @RequestParam 就是去掉/{id} 在url上写?id=1
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable  Integer id){
        Article article= articleService.selectById(id);
        return Result.success(article);
    }

    /**
     * 分页查询用插件pagehelper
     */
    @GetMapping("/selectPage")
     public Result selectPage(Article article,
                              @RequestParam (defaultValue = "1") Integer pageNum,
                              @RequestParam (defaultValue = "10") Integer pageSize){
        PageInfo<Article> pageInfo= articleService.selectPage(article,pageNum,pageSize);
        return Result.success(pageInfo);
    }


}
